////////////////////////////////////////////////////////////////////////////////
// This file is part of the Mist compiler.                                    //
//                                                                            //
// Copyright (C) 2009 The Mist project                                        //
//                                                                            //
// For a list of contributors, see the COMMITTERS file.                       //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// in the COPYING file. If not, see <http://www.gnu.org/licenses/>.           //
////////////////////////////////////////////////////////////////////////////////

///
/// \file
/// Contains the CustomComputed class.
///

#ifndef COMMON_CUSTOMCOMPUTED_HPP
#define COMMON_CUSTOMCOMPUTED_HPP

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

#include <boost/optional.hpp>
using boost::optional;

#include "common/assert.hpp"
#include "common/computed.hpp"

////////////////////////////////////////////////////////////////////////////////
// Computed Class                                                             //
////////////////////////////////////////////////////////////////////////////////

/// A value can be set, but afterwards can never change. It inherits from
/// the Computed class, so it can be specified that there was an error in
/// calculation. Circular dependency is not possible with CustomComputed.
///
template <class T>
class CustomComputed : public Computed<T> {
	public:
		
		T operator=(T value);
		
		void set_error();
};

////////////////////////////////////////////////////////////////////////////////
// Template Function Implementations                                          //
////////////////////////////////////////////////////////////////////////////////

template <class T>
T CustomComputed<T>::operator=(T value) {
	assert(Computed<T>::state() != COMPUTED);
	
	set_value(value);
	
	return value;
}

template <class T>
void CustomComputed<T>::set_error() {
	assert(Computed<T>::state() != COMPUTED);
	
	Computed<T>::set_error();
}

#endif // COMMON_CUSTOMCOMPUTED_HPP
